ARCH ?= arm
BOARD ?= vexpress-a9
VER  ?= $(shell git describe --tags)
WHO  ?= "$(VER) ($(shell whoami)@$(shell hostname))\n$(shell date)"


all: image

sign:
	@echo $(WHO)

image: staticdef defconfig config
	@make config
	@cmake -G "Unix Makefiles" -B output -DARCH=$(ARCH) -DSIGNOFF=$(WHO)
	@cmake --build output
	@make checkdep
ifeq ($(ARCH), arm)
	@arm-none-eabi-objcopy -I elf32-littlearm -O binary output/AstralOS AstralOS.bin
endif
	@gzip -c AstralOS.bin > AstralOS.bin.gz

output:
	@mkdir -p output

checkdep: output staticdef defconfig config
	@cmake -G "Unix Makefiles" -B output  -DARCH=$(ARCH) --graphviz=output/dep.dot
	@dot -Tpng -o helps/pics/dependency.png output/dep.dot 

# --sd
qemu:
ifeq ($(ARCH), arm)
	qemu-system-arm -M vexpress-a9 --kernel output/AstralOS --sd output/fs.fat --serial mon:stdio  $(DBG) -net nic,macaddr=00:16:3e:00:00:01,model=lan9118
else ifeq ($(ARCH), riscv)
	qemu-system-riscv64 -nographic -machine virt -m 128M -bios none -kernel output/AstralOS  $(DBG)
else
	@echo "please set correct qemu"
endif

staticdef: output
	python3 tchain/script/static_task.py --define=kservice/task.def > output/static_task.c


config: output
	python3 tchain/script/parseconfig.py --cfg=.config > output/config.cmake
	python3 tchain/script/dvcompiler.py --board=$(BOARD) > output/devinfox.c

defconfig:
	cp processor/$(ARCH)/defconfig .config

ramfs:
	sudo dd if=/dev/zero of=fs.fat bs=512 count=4096
	sudo mkfs.fat fs.fat
	sudo chmod 777 fs.fat
	mkdir -p fs
	sudo mount fs.fat fs
	sudo cp -r helps/* fs
	sudo cp output/AstralOS fs
	sudo umount fs
	rm -rf fs
	mkdir -p output
	mv -f fs.fat output

clean:
	rm -rf output
	rm -rf *.bin *.gz

INPATH := include/innerapi
MOD ?=
extendmacro:
	@gcc -E -o output/real$(MOD).h $(INPATH)/$(MOD).h -I$(INPATH) \
	-I /home/sddy/astral/comn/Type \
	-I /home/sddy/astral/comn/Queue \
	-I /home/sddy/astral/include/kernel
	@echo please check output/real$(MOD).h